<template>
  <div class="search">
    <div class="head">
      <span
        v-for="(item, index) in Artists"
        :key="index"
        @click="handleSearchHot"
        >{{ item.first }}</span
      >
      <input
        type="text"
        placeholder="音乐/歌手"
        class="input"
        @keydown="onEnter"
        ref="searchInput"
      />
    </div>
    <div v-if="searchList.length > 0" style="height:91%">
      <SongsListVue :searchList="searchList"/>
    </div>
    <div v-else class="nothing">这里什么也没有~</div>
  </div>
</template>
<script setup>
import { onMounted, ref } from "vue"
import { searchHot, search } from "@/api/index.js"
import SongsListVue from "../SongsList/SongsList.vue"

// 热搜词条
const Artists = ref([])
// 搜索结果列表
const searchList = ref([])
// 搜索框的值
const searchInput = ref("")

const Enter = async () => {
  const searchResponse = await search(searchInput.value.value)
  searchList.value = searchResponse.data.result.songs
}
const onEnter = async (event) => {
  if (event.keyCode === 13) {
    searchList.value = []
    Enter()
  }
}

const handleSearchHot = async (event) => {
  // console.log(event.target.textContent);
  searchInput.value.value = event.target.textContent
  searchList.value = []
  Enter()
}

onMounted(async () => {
  const response = await searchHot()
  const searchHotRes = response.data.result.hots
  Artists.value = searchHotRes.slice(0, 5)
  // console.log(Artists.value);
})
</script>
<style scoped lang="less">
.list-item:hover {
  .play2 {
    display: inline-block;
  }
}
.play2 {
  display: none;
}
::v-deep .el-scrollbar__bar.is-vertical {
  background-color: #514f4ff7;
}
::v-deep .el-scrollbar__thumb {
  background-color: #fff;
}
.list-header {
  display: flex;
  height: 50px;
  border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
  line-height: 50px;
  color: #fff;
  font-size: 14px;
  padding-left: 40px;
  .list-name {
    flex: 1;
    display: inline-block;
  }
  .list-art {
    width: 200px;
    display: inline-block;
  }
  .list-time {
    width: 200px;
    display: inline-block;
  }
}
.list-item {
  display: flex;
  height: 50px;
  border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
  line-height: 50px;
  color: hsla(0, 0%, 100%, 0.6);
  font-size: 14px;
  .list-num {
    display: flex;
    justify-content: center;
    height: 50px;
    line-height: 50px;
    width: 30px;
    margin-right: 10px;
  }
  .list-name {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .play2 {
      margin-right: 5px;
      width: 35px;
      height: 35px;
      cursor: pointer;
    }
  }
  .list-art {
    width: 200px;
    display: inline-block;
  }
  .list-time {
    span {
      width: 200px;
      display: block;
      align-items: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      margin-right: 6px;
    }
  }
}
.search {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  .nothing {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 545px;
    font-size: 14px;
    color: hsla(0, 0%, 100%, 0.6);
  }
  .head {
    display: flex;
    height: 40px;
    padding: 10px 15px;
    background: rgba(0, 0, 0, 0.2);
    .input {
      flex: 1;
      height: 40px;
      padding: 0 15px;
      border: 1px solid hsla(0, 0%, 100%, 0.6);
      background: transparent;
      color: #fff;
      font-size: 14px;
    }
    .input::placeholder {
      color: hsla(0, 0%, 100%, 0.6);
    }
    span {
      line-height: 40px;
      margin-right: 15px;
      cursor: pointer;
      color: hsla(0, 0%, 100%, 0.6);
      font-size: 14px;
    }
    span:hover {
      color: #fff;
    }
  }
}
</style>
